package com.sunelec.controller;

import com.sunelec.pojo.Power;
import com.sunelec.pojo.PowerCenter;
import com.sunelec.service.ShowInfoService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import com.sunelec.pojo.Relationship;
import com.sunelec.pojo.Users;
import com.sunelec.service.CentralizedService;
import com.sunelec.utils.HttpClientUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: 郑明利
 * @Description:
 * @Date: Create in 23:53 2018/06/02
 * @Modified By:
 */
@Controller
public class CentralizedControl {

    @Autowired
    private CentralizedService centralizedService;

    @Autowired
    ShowInfoService showInfoService;

    @RequestMapping("/centralizedControl")
    public String CentralizedControl(HttpSession session, Model model, PowerCenter powerCenter){
        if(session.getAttribute("users")==null){
            model.addAttribute("msg","请先进行登录");
            return "/login";
        }else{
            Users users = (Users) session.getAttribute("users");
            String url = "http://apis.map.qq.com/ws/geocoder/v1/";
            Map<String, String> param = new HashMap<>();
            JSONArray re = new JSONArray();
            if(users.getType() == 3){
                //先根据username找到power
                List<Relationship> relationship = centralizedService.getRelationshipWithType3(users.getUsersname());
                //遍历所有电站Id得到电站位置坐标
                for(Relationship r : relationship) {
                    param.clear();
                    Power p = showInfoService.getPowerInfo(r.getPowerId());//根据powerid查询power

                    re.add(getJSONObject(p, param, url));
                }
            } else if(users.getType() == 2) {
                //先根据username找到power
                List<Relationship> relationship = centralizedService.getRelationshipWithType2(users.getUsersname());
                //遍历所有电站Id得到电站位置坐标
                for(Relationship r : relationship) {
                    param.clear();
                    Power p = showInfoService.getPowerInfo(r.getPowerId());//根据powerid查询power

                    re.add(getJSONObject(p, param, url));
                }
            } else {
                List<Power> powers = centralizedService.getRelationshipWithType1();
                for(Power p : powers) {
                    param.clear();
                    re.add(getJSONObject(p, param, url));
                }
            }
            System.out.println(re);
            model.addAttribute("powerCenter", powerCenter);
            model.addAttribute("re", re);
            return "/centralizedControl";
        }
    }

    public JSONObject getJSONObject(Power p, Map param, String url) {
        String[] attr = p.getPowerLocal().split("\\s+");
        param.put("address", attr[0]);
        param.put("key", "3IBBZ-26OKO-XDRWB-SANAW-TETFV-U2F2E");

        String json = HttpClientUtil.doGet(url, param);
        JSONObject jsonObject1 = JSONObject.fromObject(json);
        String result = jsonObject1.getString("result");
        JSONObject jsonObject2 = JSONObject.fromObject(result);
        String location = jsonObject2.getString("location");
        JSONObject jsonObject3 = JSONObject.fromObject(location);
        String[] attr2 = {jsonObject3.getString("lng"),jsonObject3.getString("lat")};
        JSONObject jsonObject4 = new JSONObject();
        jsonObject4.put("address",attr[0]);
        jsonObject4.put("location",attr2);
        return jsonObject4;
    }
}
